/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-28     RT-Thread    first version
 */

#include <rtthread.h>

#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5

#define EVENT_FLAG3 (1<<3)
#define EVENT_FLAG5 (1<<5)


#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

static struct rt_event event;
static char thread1_stack[1024];
static struct rt_thread thread1;
static char thread2_stack[1024];
static struct rt_thread thread2;


static void thread1_recv_event(void *param){
    rt_uint32_t e;
    if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e) == RT_EOK){
        rt_kprintf("thread1: or recv event 0x%x\n",e);
    }
    rt_kprintf("thread 1 delays 1s to prepare the second\n");
    rt_thread_mdelay(1000);
    if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e) == RT_EOK){
        rt_kprintf("thread1: and recv event 0x%x\n",e);
    }
    rt_kprintf("thread 1 leave\n");

}

static void thread2_send_event(void *param){
    rt_kprintf("thread2 send event3\n");
    rt_event_send(&event, EVENT_FLAG3);
    rt_thread_mdelay(200);

    rt_kprintf("thread2 send event5\n");
    rt_event_send(&event, EVENT_FLAG5);
    rt_thread_mdelay(200);

    rt_kprintf("thread2 send event3\n");
    rt_event_send(&event, EVENT_FLAG3);
    rt_kprintf("thread2 leave\n");
}


int main(void)
{

    rt_err_t result;
    result = rt_event_init(&event,"event",RT_IPC_FLAG_FIFO);
    if (result !=RT_EOK){
        rt_kprintf("error\n");
        return -1;
    }
    rt_thread_init(&thread1,"thread1",thread1_recv_event,RT_NULL,&thread1_stack[0],sizeof(thread1_stack),THREAD_PRIORITY - 1,THREAD_TIMESLICE);
    rt_thread_startup(&thread1);

    rt_thread_init(&thread2,"thread2",thread2_send_event,RT_NULL,&thread2_stack[0],sizeof(thread2_stack),THREAD_PRIORITY ,THREAD_TIMESLICE);
    rt_thread_startup(&thread2);

    return RT_EOK;
}
